home *** CD-ROM | disk | FTP | other *** search
/ Die Speccy' 97 / Die Speccy' 97.iso / amiga_system / the_aminet / comm / bbs / s342q07.lha / sysarc.c < prev    next >
C/C++ Source or Header  |  1995-05-10  |  10KB  |  378 lines

  1. /*
  2. *       sysarc.c
  3. *
  4. * This file contains the system dependent code for deARCing files for
  5. * download.
  6. */
  7. /*
  8. *       history
  9. *
  10. * 91Oct07 HAW Netcache stuff.
  11. * 88Jun08 HAW Created.
  12. */
  13. #define SYSTEM_DEPENDENT
  14. #include "ctdl.h"
  15. #include "dos.h"
  16. /*#include "sys\stat.h" */
  17. /* #include "process.h" */
  18. /*
  19. *       Contents
  20. *
  21. * ArcInit()   initialize for deARCing
  22. * CompAvailable()   is compression available for this type?
  23. * SendArcFiles()    get arc name, send contents specified
  24. * # MakeTempDir()   Create and move into a temporary dir
  25. *
  26. *   # == local for this implementation only
  27. */
  28. extern aRoom roomBuf;
  29. extern MessageBuffer msgBuf;
  30. extern CONFIG  cfg;
  31. extern char whichIO;
  32. char TDirBuffer[120];
  33. char cmdbuf[120];  /* command line buffer */
  34. /*
  35. * This table must be kept in sync with Formats[] in Misc.c
  36. */
  37. DeCompElement DeComp[] =
  38.   {
  39.     { "delha.sys", "L. Lha",   NULL, NULL, NULL },
  40.     { "delzh.sys", "P. Lzh",   NULL, NULL, NULL },
  41.     { "dezoo.sys", "Z. Zoo",   NULL, NULL, NULL },
  42.     { "dearc.sys", "A. Arc",   NULL, NULL, NULL },
  43.  
  44.   };
  45.  
  46. int Jsystem(char *);
  47. static void CheckCompressedData(char *name, int index);
  48. static void MakeTempName(void);
  49. /*
  50. * ArcInit()
  51. *
  52. * This initializes this module for handling the various variants of
  53. * compression/decompression.
  54. */
  55. void ArcInit()
  56.   {
  57.   SYS_FILE name;
  58.   int i;
  59.   for (i = 0; i < NumElems(DeComp); i++)
  60.       {
  61.     makeSysName(name, DeComp[i].FileName, &cfg.roomArea);
  62.     CheckCompressedData(name, i);
  63.  
  64.     }
  65.  
  66.   }
  67. /*
  68. * CheckCompressedData()
  69. *
  70. * This function does an initial check for existence of a configuration file.
  71. */
  72. static void CheckCompressedData(char *name, int index)
  73.   {
  74.   FILE *fd;
  75.   char work[80];
  76.   if (cfg.BoolFlags.debug) splitF(NULL,"Open Attemp:%s\n",name);
  77.   if ((fd = fopen(name, "r")) == NULL)
  78.       {
  79.     return;
  80.  
  81.     }
  82.   if (cfg.BoolFlags.debug) splitF(NULL,"Opened:%s\n",name);
  83.   if (GetAString(work, sizeof work, fd) != NULL)
  84.       {
  85.       if (cfg.BoolFlags.debug) splitF(NULL,"Read 1:%s\n",work);
  86.       if (strlen(work) != 0)DeComp[index].DeWork = strdup(work);
  87.       if (GetAString(work, sizeof work, fd) != NULL)
  88.         {
  89.         if (cfg.BoolFlags.debug) splitF(NULL,"Read 2:%s\n",work);
  90.         if (strlen(work) != 0) DeComp[index].IntWork = strdup(work);
  91.         if (GetAString(work, sizeof work, fd) != NULL)
  92.           {
  93.           if (cfg.BoolFlags.debug) splitF(NULL,"Read 3:%s\n",work);
  94.           if (strlen(work) != 0) DeComp[index].CompWork = strdup(work);
  95.           };
  96.         };
  97.  
  98.       };
  99.   fclose(fd);
  100.  
  101.   }
  102. /*
  103. * SendArcFiles()
  104. *
  105. * This function gets arc filename, send specified contents.
  106. */
  107. void SendArcFiles(int protocol)
  108.   {
  109.   SYS_FILE name;
  110.   char     *temp;
  111.   int  NumFiles, format;
  112.   extern long netBytes;
  113.   getNormStr("ARCHFN", name, SIZE_SYS_FILE, 0);
  114.   if (strLen(name) == 0) return;
  115.   if (!setSpace(&roomBuf)) return;
  116.   temp = getcwd(NULL, 100);
  117.   sPrintf(msgBuf.mbtext, "%s/%s", temp, name);
  118.   homeSpace();
  119.   if (access(msgBuf.mbtext, 4) == -1)
  120.     {
  121.     for (NumFiles = 0; NumFiles < NumElems(DeComp); NumFiles++)
  122.       {
  123.       sPrintf(msgBuf.mbtext, "%s/%s.%s", temp,name, Formats[NumFiles].Format);
  124.       if (access(msgBuf.mbtext, 4) == 0) break;
  125.  
  126.       };
  127.  
  128.     };
  129.   if( access(msgBuf.mbtext,4) == -1 )
  130.     {
  131.     mPrintf("%s does not exist.\n ", name);
  132.     free(temp);
  133.     return;
  134.  
  135.     };
  136.   if ((format = CompressType(msgBuf.mbtext)) == -1)
  137.   format = 0;
  138.   if (DeComp[format].DeWork == NULL)
  139.       {
  140.     mPrintf("Sorry, de%sing not supported here.\n ",
  141.     Formats[format].Format);
  142.     return;
  143.  
  144.     }
  145.   sPrintf(msgBuf.mbtext, "%s %s/%s ",
  146.   DeComp[format].DeWork, temp, name);
  147.   getNormStr("ARCHMS", msgBuf.mbtext, 100, 0);
  148.   if (strLen(msgBuf.mbtext) == 0) strCpy(msgBuf.mbtext, ALL_FILES);
  149.   MakeTempDir();      /* Create and drop into */
  150.   mPrintf("One moment, please ...\n ");
  151.   sprintf(cmdbuf, "%s %s/%s %s",
  152.   DeComp[format].DeWork, temp, name, msgBuf.mbtext);
  153.   Jsystem(cmdbuf);
  154.   free(temp);         /* Don't need this any longer, so kill it now */
  155.   netBytes = 0l;
  156.   NumFiles = wildCard(getSize, ALL_FILES, FALSE, "", FALSE);
  157.   if (NumFiles <= 0)
  158.       {
  159.     mPrintf("Sorry, no files matched that deARC command.\n ");
  160.  
  161.     }
  162.   else
  163.       {
  164.     if ( getYesNo("MREFFI") )
  165.     if (TranAdmin(protocol, NumFiles))
  166.     TranSend(protocol, transmitFile, ALL_FILES, "", FALSE);
  167.     /*
  168.     * This call is due to a regretable side-effect of losing
  169.     * carrier while reading ARC-derived files.
  170.     */
  171.     chdir(TDirBuffer);
  172.     wildCard(DelFile, ALL_FILES, FALSE, "", FALSE);
  173.  
  174.     }
  175.   homeSpace();
  176.   rmdir(TDirBuffer);
  177.  
  178.   }
  179. /*
  180. * MakeTempDir()
  181. *
  182. * This function will create and move into a temporary directory.
  183. */
  184. void MakeTempDir()
  185.   {
  186.   MakeTempName();
  187.   if (mkdir(TDirBuffer) != 0) mPrintf("System error with mkdir!\n ");
  188.   chdir(TDirBuffer);
  189.   getcwd(TDirBuffer, 120);
  190.  
  191.   }
  192. /*
  193. * MakeTempName()
  194. *
  195. * This function gets a temporary name.
  196. */
  197. static void MakeTempName()
  198.   {
  199.   static char *seed = "tmp_%04d";
  200.   int i = 0;
  201.   sPrintf(TDirBuffer, seed, i++);
  202.   while (access(TDirBuffer, 0) != -1) sPrintf(TDirBuffer, seed, i++);
  203.   }
  204.  
  205. #define BAT_FILE  "%s\nif warn \n echo \"1\" >%s\nelse\n echo \"0\" >%s\nendif\n"
  206.  
  207. /*
  208. * FileIntegrity()
  209. *
  210. * This function does a file integrity check.
  211. * Note: we don't use spawn() since it adds 3K to the .EXE.
  212. */
  213. char FileIntegrity(char *filename)
  214.   {
  215.   char *c, *d, check[90], bad, NameUsed = FALSE;
  216.   int format;
  217.   FILE *fd;
  218.   if ((c = strchr(filename, '.')) != NULL)
  219.       {
  220.     if ((format = CompressType(filename)) == -1) return TRUE;
  221.     if (format > COMP_MAX - 1)                   return TRUE;
  222.     if (DeComp[format].IntWork == NULL)          return TRUE;
  223.     if (!getYesNo("INTEGR"))return TRUE;
  224.     Output_Citadel_Message("ONEMOM",NULL, NULL, NULL);
  225.     MakeTempName();
  226.     strCat(TDirBuffer, ".bat");
  227.     if (cfg.BoolFlags.debug) splitF(NULL,"File Open:%s\n",TDirBuffer);
  228.     fd = fopen(TDirBuffer, "w");
  229.     sprintf(check,"Execute %s",TDirBuffer);
  230.     MakeTempName();
  231.     /*
  232.     * Manually copy.  If we encounter "%g" then replace it with the
  233.     * name of the file to test.  If we never encounter it, then
  234.     * remember to append it to the end of the string after the copy
  235.     * has finished (NameUsed).
  236.     */
  237.     for (c = DeComp[format].IntWork, d = msgBuf.mbtext; *c; c++)
  238.         {
  239.       if (*c == '%' && *(c + 1) == 'g')
  240.           {
  241.         strCpy(d, filename);
  242.         NameUsed = TRUE;
  243.         while (*d) d++;
  244.         c++;      /* this will get us over the %g. */
  245.  
  246.         }
  247.       else *d++ = *c;
  248.  
  249.       }
  250.     *d++ = ' ';   /* harmless space padding. */
  251.     *d = 0;
  252.     if (!NameUsed) strCat(msgBuf.mbtext, filename);
  253.     fprintf(fd, BAT_FILE, msgBuf.mbtext, TDirBuffer, TDirBuffer);
  254.     fclose(fd);
  255.     Jsystem(check);
  256.     unlink(check);
  257.     if (cfg.BoolFlags.debug) splitF(NULL,"File Open:%s\n",TDirBuffer);
  258.     bad = TRUE;
  259.     if ((fd = fopen(TDirBuffer, "r")) != NULL)
  260.       {
  261.       if (fgets(check, sizeof check, fd) != NULL) bad = *check != '0';
  262.       fclose(fd);
  263.       unlink(TDirBuffer);
  264.       };
  265.     if (bad) return (char)!getYesNo("FAILIC");
  266.     else Output_Citadel_Message("PASSED",NULL, NULL, NULL);
  267.  
  268.     }
  269.   return TRUE;
  270.  
  271.   }
  272. /*
  273. * CompAvailable()
  274. *
  275. * This function determines if the given compression type has a compression
  276. * method defined.
  277. */
  278. char CompAvailable(char CompType)
  279.   {
  280.   return (char)(DeComp[CompType - 1].CompWork != NULL);
  281.  
  282.   }
  283. /*
  284. * NetDeCompress()
  285. *
  286. * This decompresses files for network dissection.
  287. */
  288. void NetDeCompress(char CompType, SYS_FILE fn)
  289.   {
  290.   char *c;
  291.   SYS_FILE dir;
  292.   char pwd[100];
  293.   if (DeComp[CompType - 1].DeWork == NULL) return;
  294.   strCpy(dir, fn);
  295.   if ((c = strchr(dir, '/')) != NULL)
  296.       {
  297.     *c++ = 0;
  298.     chdir(dir);
  299.  
  300.     }
  301.   else c = dir;
  302.   getcwd(pwd, 100);
  303.   sPrintf(lbyte(pwd), pwd[strlen(pwd) - 1] == '/' ? "%s" : "/%s", c);
  304.   homeSpace();
  305.   MakeTempDir();
  306.   sprintf(cmdbuf,"%s %s", DeComp[CompType - 1].DeWork, pwd);
  307.   Jsystem(cmdbuf);
  308.   homeSpace();
  309.  
  310.   }
  311. /*
  312. * KillNetDeCompress()
  313. *
  314. * This function clears out the files from the decompression.
  315. */
  316. void KillNetDeCompress()
  317.   {
  318.   chdir(TDirBuffer);
  319.   wildCard(DelFile, ALL_FILES, FALSE, "", FALSE);
  320.   homeSpace();
  321.   rmdir(TDirBuffer);
  322.  
  323.   }
  324. /*
  325. * Compress()
  326. *
  327. * This compresses the given files into the given archive file using the
  328. * given file.
  329. */
  330. void Compress(char CompType, char *Files, char *ArcFileName)
  331.   {
  332.   if (!CompAvailable(CompType)) return;
  333.   sprintf(cmdbuf, "%s %s %s", DeComp[CompType - 1].CompWork, ArcFileName, Files);
  334.   Jsystem(cmdbuf);
  335.  
  336.   }
  337. /*
  338. * GetUserCompression()
  339. *
  340. * This function handles a request for a compression protocol.  This can be
  341. * called either for a sysop (when selecting for Mass Transfers) or by a user
  342. * (eventually) for .RC.
  343. *
  344. * This returns the protocol selected (LHA_COMP, etc) or NO_COMP if none
  345. * was selected or if none are available.
  346. */
  347. int GetUserCompression()
  348.   {
  349.   int  rover, count = 0, x;
  350.   MenuId   id;
  351.   char *CompOpts[] =
  352.       {
  353.     " ", " ", " ", " ", ""
  354.     };
  355.     for (rover = 0; rover < NumElems(DeComp); rover++) {
  356.     if (DeComp[rover].CompWork != NULL) {
  357.     count++;
  358.     ExtraOption(CompOpts, DeComp[rover].MenuEntry);
  359.     if (whichIO == MODEM) mPrintf("%s\n ", DeComp[rover].MenuEntry);
  360.     }
  361.     }
  362.     if (count == 0) {
  363.     SysopError(NO_MENU, "No Compression methods");
  364.     return NO_COMP;
  365.     }
  366.     id = RegisterSysopMenu("Compress.mnu", CompOpts, " Compression ");
  367.     SysopMenuPrompt(id, "\n Compression method: ");
  368.     x = GetSysopMenuChar(id);
  369.     CloseSysopMenu(id);
  370.     switch (x) {
  371.     case 'P': return LZH_COMP;
  372.     case 'Z': return ZOO_COMP;
  373.     case 'L': return LHA_COMP;
  374.     case 'A': return ARC_COMP;
  375.     default:  return NO_COMP;
  376.     }
  377.     }
  378.